package com.koron.tool.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.tool.bean.DictData;
import com.koron.tool.bean.DictItem;
import com.koron.tool.mapper.DictDataMapper;
import com.koron.tool.service.DictDataService;
import com.koron.tool.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: liaodf
 * @Date: 2021/10/27 10:59
 */
@Slf4j
@Service
public class DictDataServiceImpl implements DictDataService {

    @Override
    @TaskAnnotation("getDict")
    public Response getDict(SessionFactory factory,String code) {
        DictDataMapper mapper = factory.getMapper(DictDataMapper.class);
        List<DictData> dictDataList = mapper.getDict(code);
        return Response.ok(dictDataList);
    }

    @Override
    @TaskAnnotation("save")
    public Response save(SessionFactory factory, EamUser user, DictData dictData) {
        try {
            DictDataMapper mapper = factory.getMapper(DictDataMapper.class);
            if (StringUtils.isEmpty(dictData.getId())) {
                BeanUtil.add(user, dictData);
                mapper.insert(dictData);
            } else {
                BeanUtil.update(user, dictData);
                mapper.update(dictData);
            }
            return Response.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }

    @Override
    @TaskAnnotation("saveItem")
    public Response saveItem(SessionFactory factory, EamUser user, DictItem dictItem) {
        try {
            if (StringUtils.isEmpty(dictItem.getDictCode())){
                return Response.fail("字典编码不能为空");
            }
            DictDataMapper mapper = factory.getMapper(DictDataMapper.class);
            if (StringUtils.isEmpty(dictItem.getId())) {
                BeanUtil.add(user, dictItem);
                mapper.insertItem(dictItem);
            } else {
                BeanUtil.update(user, dictItem);
                mapper.updateItem(dictItem);
            }
            return Response.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }

    @Override
    @TaskAnnotation("delete")
    public Response delete(SessionFactory factory, EamUser user, String id) {
        try {
            DictDataMapper mapper = factory.getMapper(DictDataMapper.class);
            DictData dictData = mapper.getDictData(id);
            List<DictItem> dictItemList = mapper.getDictItemByDictCode(dictData.getCode());
            if (null !=dictItemList && dictItemList.size()>0){
                return Response.fail("存在字典项，无法删除");
            }
            Integer value = mapper.delete(id);
            return Response.ok(value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }

    @Override
    @TaskAnnotation("deleteItem")
    public Response deleteItem(SessionFactory factory, EamUser user, String id) {
        try {
            DictDataMapper mapper = factory.getMapper(DictDataMapper.class);
            Integer value = mapper.deleteItem(id);
            return Response.ok(value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }
}
